package com.easygo.controller;

import com.easygo.service.IContentCategoryService;
import com.easygo.utils.PageUtils;
import com.xuexi.easygo.pojo.ContentCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class ContentCategoryController {

    @Autowired
    IContentCategoryService categoryService;

    @RequestMapping("/contentCategory_page")
    public PageUtils<ContentCategory> getcontentCategorys(@RequestParam(defaultValue = "1",required = false) Integer pageIndex,
                                                          @RequestParam(defaultValue = "5",required = false) Integer pageSize,
                                                          @RequestParam(defaultValue = "",required = false) String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "%" + name + "%");
        map.put("pageStart", (pageIndex - 1) * pageSize);
        map.put("pageSize", pageSize);

        System.out.println("参数集合:" + map);

        Integer totalCount = categoryService.getTotalCount(map);
        List<ContentCategory> contentCategories = categoryService.getContentCategoryPages(map);
        PageUtils<ContentCategory> pageUtils = new PageUtils<>(pageSize, pageIndex, totalCount, contentCategories);
        return pageUtils;
    }

    @RequestMapping("contentCategory_add")
    public int addContentCategory(@RequestBody ContentCategory category){
        return categoryService.addContentCategory(category);
    }

    @RequestMapping("contentCategory_getById")
    public ContentCategory getById(Integer id){
        return categoryService.getById(id);
    }

    @RequestMapping("contentCategory_update")
    public int updateContentCategory(ContentCategory category){
        return categoryService.updateContentCategory(category);
    }

    @RequestMapping("contentCategory_delete")
    public int deleteContentCategory(Integer id){
        return categoryService.deleteContentCategory(id);
    }

    @RequestMapping("contentCategory_list")
    public List<ContentCategory> getContentCategorys(){
        return categoryService.getContentCategory();
    }
}
